package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.CardStock;
import com.turing.post.cardmanagement.util.CardStockDao;
import com.turing.post.salesmanage.util.SaleOrder;
import com.turing.post.salesmanage.util.SaleOrderCardDetail;
import com.turing.post.salesmanage.util.SaleOrderCardDetailDao;
import com.turing.post.salesmanage.util.SaleOrderDao;
import com.turing.post.salesmanage.util.SaleOrderRealNameDetail;
import com.turing.post.salesmanage.util.SaleOrderRealNameDetailBusiness;
import com.turing.post.salesmanage.util.SaleOrderRealNameDetailDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 持卡人实名明细
 * 
 * @author zxp
 * @Dec 23, 2012
 */
@Stateless(mappedName = "salesmanage.impl.SaleOrderRealNameDetailBusinessImpl")
@Remote
@Local
public class SaleOrderRealNameDetailBusinessImpl implements
		SaleOrderRealNameDetailBusiness {

	@EJB(mappedName = "salesmanage.impl.SaleOrderRealNameDetailDaoImpl")
	private SaleOrderRealNameDetailDao saleOrderRealNameDetailDaoImpl;

	@EJB(mappedName = "salesmanage.impl.SaleOrderCardDetailDaoImpl")
	private SaleOrderCardDetailDao saleOrderCardDetailDaoImpl;

	@EJB(mappedName = "salesmanage.impl.SaleOrderDaoImpl")
	private SaleOrderDao saleOrderDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.CardStockDaoImpl")
	private CardStockDao cardStockDaoImpl;

	@Override
	public void insertSaleOrderRealNameDetailInfo(
			SaleOrderRealNameDetail saleOrderRealNameDetail)
			throws PCPSApplicationException {
		if (saleOrderRealNameDetail == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"SaleOrderRealNameDetail为NULL");
		}
		// 销售单
		SaleOrder saleOrder = saleOrderDaoImpl
				.getSaleOrderBySaleOrderId(saleOrderRealNameDetail
						.getSaleOrderId());
		if (saleOrder == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "销售单为空!");
		}
		if (PCPSUtil.isEmpty(saleOrderRealNameDetail.getBatchNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "批次号为空!");
		}
		if (PCPSUtil.isEmpty(saleOrderRealNameDetail.getCardSeqNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "卡顺序号为空!");
		}
		// 状态
		if (!saleOrder.getOrderStatus().equals(
				SaleOrder.SALEORDER_ORDERSTATUS_INPUT)) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"只有录入状态才可新增持卡人实名信息!");
		}
		// 销售单售卡
		HashMap<String, String> cardMp = new HashMap<String, String>();
		List<SaleOrderCardDetail> saleOrderCardDetailList = saleOrderCardDetailDaoImpl
				.getSaleOrderCardDetailListBySaleOrderId(saleOrder
						.getSaleOrderId());
		for (SaleOrderCardDetail socd : saleOrderCardDetailList) {
			List<CardStock> cardStockList = cardStockDaoImpl
					.getCardStockListBySaleBranchNoAndStatusAndCredseqno(socd
							.getStartCardSeqNo(), socd.getEndCardSeqNo(),
							CardStock.CARDSTOCK_STATUS_NORMAL, saleOrder
									.getSaleBranchNo());
			for (CardStock cs : cardStockList) {
				cardMp.put(cs.getCardSeqNo(), cs.getCardNo());
			}
		}

		// 判断卡是否存在库存里,获取卡号
		// 销售单状态，已激活去activecard中查询数据
		// if (saleOrder.getOrderStatus().equals(
		// SaleOrder.SALEORDER_ORDERSTATUS_ACTIVATED)) {
		// ActivateCard ac = activateCardDaoImpl.checkCardExistInActivateCard(
		// saleOrderRealNameDetail.getBatchNo(),
		// saleOrderRealNameDetail.getCardSeqNo());
		// if (ac == null) {
		// throw new SalesManageApplicationException(
		// SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
		// SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
		// "输入卡信息未在系统库存中查询到!");
		// } else {
		// saleOrderRealNameDetail.setCardNo(ac.getCardNo());
		// }
		// } else {
		// 详细卡列表
		if (cardMp.get(saleOrderRealNameDetail.getCardSeqNo()) == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "输入卡顺序号"
							+ saleOrderRealNameDetail.getCardSeqNo()
							+ "未在销售单售卡明细中查询到!");
		} else {
			saleOrderRealNameDetail.setCardNo(cardMp
					.get(saleOrderRealNameDetail.getCardSeqNo()));
		}
		// }
		if (saleOrderRealNameDetail.getCustomId() == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "持卡人为空!");
		}
		saleOrderRealNameDetailDaoImpl
				.insertSaleOrderRealNameDetail(saleOrderRealNameDetail);
	}

	@Override
	public void updateSaleOrderRealNameDetailInfo(
			SaleOrderRealNameDetail saleOrderRealNameDetail)
			throws PCPSApplicationException {
		SaleOrderRealNameDetail _saleOrderRealName = saleOrderRealNameDetailDaoImpl
				.getSaleOrderRealNameDetailByPrimaryKey(saleOrderRealNameDetail);
		if (_saleOrderRealName == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"待修改持卡人实名信息数据库中不存在!");
		}
		// 对象赋值
		_saleOrderRealName.setBatchNo(saleOrderRealNameDetail.getBatchNo());
		_saleOrderRealName.setCardNo(saleOrderRealNameDetail.getCardNo());
		_saleOrderRealName.setCardSeqNo(saleOrderRealNameDetail.getCardSeqNo());
		_saleOrderRealName.setCustomId(saleOrderRealNameDetail.getCustomId());
		// 销售单
		SaleOrder saleOrder = saleOrderDaoImpl
				.getSaleOrderBySaleOrderId(_saleOrderRealName.getSaleOrderId());

		// 状态
		if (!saleOrder.getOrderStatus().equals(
				SaleOrder.SALEORDER_ORDERSTATUS_INPUT)) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"只有录入状态才可修改持卡人实名信息!");
		}

		// 销售单售卡
		HashMap<String, String> cardMp = new HashMap<String, String>();
		List<SaleOrderCardDetail> saleOrderCardDetailList = saleOrderCardDetailDaoImpl
				.getSaleOrderCardDetailListBySaleOrderId(saleOrder
						.getSaleOrderId());
		for (SaleOrderCardDetail socd : saleOrderCardDetailList) {
			List<CardStock> cardStockList = cardStockDaoImpl
					.getCardStockListBySaleBranchNoAndStatusAndCredseqno(socd
							.getStartCardSeqNo(), socd.getEndCardSeqNo(),
							CardStock.CARDSTOCK_STATUS_NORMAL, saleOrder
									.getSaleBranchNo());
			for (CardStock cs : cardStockList) {
				cardMp.put(cs.getCardSeqNo(), cs.getCardNo());
			}
		}
		// 判断卡是否存在库存里,获取卡号
		// 销售单状态，已激活去activecard中查询数据
		// if (saleOrder.getOrderStatus().equals(
		// SaleOrder.SALEORDER_ORDERSTATUS_ACTIVATED)) {
		// ActivateCard ac = activateCardDaoImpl.checkCardExistInActivateCard(
		// saleOrderRealNameDetail.getBatchNo(),
		// saleOrderRealNameDetail.getCardSeqNo());
		// if (ac == null) {
		// throw new SalesManageApplicationException(
		// SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
		// SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
		// "输入卡信息未在系统库存中查询到!");
		// } else {
		// saleOrderRealNameDetail.setCardNo(ac.getCardNo());
		// }
		// } else {
		// 详细卡列表
		if (cardMp.get(saleOrderRealNameDetail.getCardSeqNo()) == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "输入卡顺序号"
							+ saleOrderRealNameDetail.getCardSeqNo()
							+ "未在销售单售卡明细中查询到!");
		} else {
			saleOrderRealNameDetail.setCardNo(cardMp
					.get(saleOrderRealNameDetail.getCardSeqNo()));
		}
		// 修改
		saleOrderRealNameDetailDaoImpl
				.updateSaleOrderRealNameDetail(_saleOrderRealName);
	}

	@Override
	public void deleteSaleOrderRealNameDetailInfo(
			SaleOrderRealNameDetail saleOrderRealNameDetail)
			throws PCPSApplicationException {
		// 销售单
		SaleOrder saleOrder = saleOrderDaoImpl
				.getSaleOrderBySaleOrderId(saleOrderRealNameDetail
						.getSaleOrderId());
		// 状态
		if (!saleOrder.getOrderStatus().equals(
				SaleOrder.SALEORDER_ORDERSTATUS_INPUT)) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"只有录入状态才可修改持卡人实名信息!");
		}

		// 删除
		Integer deletecount = saleOrderRealNameDetailDaoImpl
				.deleteSaleOrderRealNameDetail(saleOrderRealNameDetail);
		if (deletecount < 0) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"删除销售单持卡人实名明细失败，判断传递参数是否正确!");
		}
	}

	@Override
	public void deleteSaleOrderRealNameDetailByOrderId(BigInteger saleOrderId)
			throws PCPSApplicationException {
		// 销售单
		SaleOrder saleOrder = saleOrderDaoImpl
				.getSaleOrderBySaleOrderId(saleOrderId);
		// 状态
		if (!saleOrder.getOrderStatus().equals(
				SaleOrder.SALEORDER_ORDERSTATUS_INPUT)) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"只有录入状态才可修改持卡人实名信息!");
		}
		// 删除
		Integer deletecount = saleOrderRealNameDetailDaoImpl
				.deleteSaleOrderRealNameDetailByOrderId(saleOrderId);
		if (deletecount < 0) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"删除销售单持卡人实名明细失败，判断传递参数是否正确!");
		}
	}

}
